package com.asia5b.a5mall.shop.activity

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.logic.viewmodel.shop.ApplyShopActivityVM
import com.asia5b.a5mall.middleware.model.shop.ApplyShopResModel
import com.asia5b.a5mall.middleware.base.BaseFragmentPagerAdapter
import com.asia5b.a5mall.middleware.utils.dddLog
import com.asia5b.a5mall.middleware.utils.pop
import com.asia5b.a5mall.middleware.utils.sNewStanceFragment
import com.asia5b.a5mall.middleware.utils.showMessage
import com.asia5b.a5mall.shop.databinding.ActivityApplyshopBinding
import com.asia5b.a5mall.shop.fragment.ApplyStepFragment
import kotlinx.android.synthetic.main.activity_applyshop.*

/**
 * Created by zhangyuncai on 2020/6/28.
 * 商家入驻
/*
https://apimall.asiajy.com/base/pages/COOPERATION_AGREEMENT/articles

https://apimall.asiajy.com/buyer/shops/apply/step1

https://apimall.asiajy.com/buyer/shops/apply/step2

https://apimall.asiajy.com/buyer/shops/apply/step3

https://apimall.asiajy.com/buyer/shops/apply/step4
https://apimall.asiajy.com/buyer/goods/categories/0/children


*/
 */
@Route(path = ActCons.ApplyShopActivity)
class ApplyShopActivity : SimpleActivity<ApplyShopActivityVM, ActivityApplyshopBinding>() {
    override val viewModelClazz: Class<ApplyShopActivityVM>
        get() = ApplyShopActivityVM::class.java

    override fun getDataBinding(): ActivityApplyshopBinding {
        return ActivityApplyshopBinding.inflate(layoutInflater)
    }
    var applyShopResModel = ApplyShopResModel()
    override var sIsDark = true
    val mViewPager by lazy { vp_applyshop }
    val fragments = arrayListOf<ApplyStepFragment>()

    fun getNotNullApplyShopResModel(): ApplyShopResModel {
        applyShopResModel.checkNull()
        return applyShopResModel

    }

    override fun initView() {
        addFragment()
        getShopsApply()
    }

    override fun startObserver() {
        mViewModel.getShopsApplyLiveData.observe(this, Observer {
            view_applyshop_empty.visibility = View.GONE//todo 必须先隐藏,否则viewpager不设置视图
            applyShopResModel = it
            setCurrentIten()
            setView()
        })
        mViewModel.putShopsApplyLiveData.observe(this, Observer {
            view_applyshop_empty.visibility = View.GONE//todo 必须先隐藏,否则viewpager不设置视图
            applyShopResModel = it[0] as ApplyShopResModel
            val step=it[1] as String
            when (step) {
                "step1" -> {
                    mViewPager.setCurrentItem(1)
                }
                "step2" -> {
                    mViewPager.setCurrentItem(2)
                }
                "step3" -> {
                    mViewPager.setCurrentItem(3)
                }
                "step4" -> {
                    showMessage("申请开店成功，等待审核...")
                    pop()
                }
                else -> {
                    mViewPager.setCurrentItem(0)
                }
            }
            setView()
        })
    }

    /**
     * 获取上次提交的信息
     */
    fun getShopsApply() {
        mViewModel.getShopsApply()

    }

    fun setCurrentIten() {
        var currentItem = 0
        val notNullApplyShopResModel = getNotNullApplyShopResModel()
        if (!TextUtils.isEmpty(notNullApplyShopResModel.step)) {
            when (notNullApplyShopResModel.step) {
                "1" -> {
                    currentItem = 0
                }
                "2" -> {
                    currentItem = 1
                }
                "3" -> {
                    currentItem = 2
                }
                "4" -> {
                    currentItem = 3
                }
            }
        }
        mViewPager.setCurrentItem(currentItem)
    }

    fun setView() {
        for (fragment in fragments) {
            fragment.setView()
        }
    }

    /**
     * 上传本次保存的信息
     */
    fun putShopsApply(step: String) {

        mViewModel.putShopsApply(step, getNotNullApplyShopResModel())
    }


    fun addFragment() {
        if (fragments.size == 0) {
            for (index in 0..3) {
                val bundle = Bundle()
                bundle.putInt("type", index)
                fragments.add(sNewStanceFragment(mActivity, ApplyStepFragment::class.java, bundle))
            }
            mViewPager.adapter = BaseFragmentPagerAdapter(supportFragmentManager, fragments as List<Fragment>?)
            mViewPager.offscreenPageLimit = fragments.size
        }

    }

    override fun initEvent() {
        view_applyshop_empty.setOnClickListener { }
        mViewPager.addOnPageChangeListener(object :ViewPager.SimpleOnPageChangeListener(){
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                dddLog("onPageScrolled:position:${position}")
                dddLog("onPageScrolled:positionOffset:${positionOffset}")
                dddLog("onPageScrolled:positionOffsetPixels:${positionOffsetPixels}")
            }
        })
    }

    override fun initData() {
    }
}